package org.ss.psci.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.ss.psci.entity.PmCheck;
import org.ss.psci.entity.PmEnd;

/**
 * Data access object (DAO) for domain model class PmCheck.
 * 
 * @see org.ss.psci.entity.PmCheck
 * @author MyEclipse Persistence Tools
 */

public class PmCheckDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(PmCheckDAO.class);
	// property constants
	public static final String MANAGER = "manager";
	public static final String CHECK_DATE = "checkDate";
	public static final String REMARK = "remark";
	public static final String TITLE = "title";
	public static final String PROJECTID = "projectId";

	public List findByProject(Object name) {
		return findByProperty(PROJECTID, name);
	}
	
	public PmCheck copyEntity(PmCheck dest, PmCheck orig) {
		if (dest == null || orig == null) {
			return new PmCheck();
		}
		if (orig.getCheckDate() != null) {
			dest.setCheckDate(orig.getCheckDate());
		}
		if (orig.getTitle() != null) {
			dest.setTitle(orig.getTitle());
		}

		if (orig.getRemark() != null) {
			dest.setRemark(orig.getRemark());
		}
		if (orig.getManager() != null) {
			dest.setManager(orig.getManager());
		}

		return dest;
	}

	protected void initDao() {
		// do nothing
	}

	public void save(PmCheck transientInstance) {
		log.debug("saving PmCheck instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(PmCheck persistentInstance) {
		log.debug("deleting PmCheck instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public PmCheck findById(java.lang.Integer id) {
		log.debug("getting PmCheck instance with id: " + id);
		try {
			PmCheck instance = (PmCheck) getHibernateTemplate().get(
					"org.ss.psci.entity.PmCheck", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(PmCheck instance) {
		log.debug("finding PmCheck instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding PmCheck instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from PmCheck as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByManager(Object manager) {
		return findByProperty(MANAGER, manager);
	}

	public List findByCheckDate(Object checkDate) {
		return findByProperty(CHECK_DATE, checkDate);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findAll() {
		log.debug("finding all PmCheck instances");
		try {
			String queryString = "from PmCheck";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public PmCheck merge(PmCheck detachedInstance) {
		log.debug("merging PmCheck instance");
		try {
			PmCheck result = (PmCheck) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(PmCheck instance) {
		log.debug("attaching dirty PmCheck instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(PmCheck instance) {
		log.debug("attaching clean PmCheck instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static PmCheckDAO getFromApplicationContext(ApplicationContext ctx) {
		return (PmCheckDAO) ctx.getBean("PmCheckDAO");
	}
}